/**  
 * Copyright (c) 2010 University of Pennsylvania. 
 *     All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an "AS
 *  IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 *  express or implied.  See the License for the specific language
 *  governing permissions and limitations under the License.
 *
 */

#define CONFIG_FILE  "config.txt"

#ifdef TOSSIM
void read_config()
{
  FILE *f;
  char line[1000], s[256], s1[1000], *p, *p1;
  ADDRESS node_id;
  uint32_t fail_time;
  int enable_count = 0;
  uint8_t t;
  uint8_t size, buffer[256];

  f = file_open(CONFIG_FILE, "r");
  while (!feof(f))
  {
    fgets(line, sizeof(line), f);
    sscanf(line, "%[a-zA-Z0-9_]=%[^$]\n", s, s1);
    while (s1[strlen(s1)-1] == '\r' || s1[strlen(s1)-1] == '\n')
      s1[strlen(s1)-1] = 0;
    strupr(s);

    if (strcmp(s, "NODE_COUNT") == 0)
      sscanf(s1, "%hu", &config.node_count);
    else if (strcmp(s, "LOAD_TREES") == 0)
      sscanf(s1, "%hhu", &config.load_trees);
    else if (strcmp(s, "RETRY_COUNT") == 0)
      sscanf(s1, "%hu", &config.retry_count);
    else if (strcmp(s, "ROUTE_METHOD") == 0)
      sscanf(s1, "%hhu", &config.route_method);
    else if (strcmp(s, "ROUTE_BEGIN") == 0)
      sscanf(s1, "%hhu", &config.start_route);
    else if (strcmp(s, "JOIN_BEGIN") == 0)
      sscanf(s1, "%hhu", &config.start_join);
    else if (strcmp(s, "ROUTE_TREESENABLED") == 0)
      sscanf(s1, "%hhu", &config.trees_enabled);
    else if (strcmp(s, "QUERY_SPEC") == 0)
    {
      size = 0;

      p = s1;
      while (p)
      {
        while (*p == ' ')
          p++;
        if (*p == 0)
          break;
        p1 = strstr(p, " ");
        if (p1)
          *p1 = 0;

        sscanf(p, "%hhu ", &buffer[size]);
        size++;

        p = p1 ? p1+1 : NULL;
      }

      if (config.load_trees == 0)
      {
        if (TOS_NODE_ID == BASESTATION_ADDR)
          enqueue_uartmsg(0, size, buffer, TOS_NODE_ID);
      } else {
        config.query_spec[config.queryspec_count].buffer_len = size;
        memcpy(&config.query_spec[config.queryspec_count].buffer, buffer, size);
        config.queryspec_count++;
      }
    } else if (strcmp(s, "SUMMARY_SPEC") == 0)
    {
      size = 0;

      p = s1;
      while (p)
      {
        while (*p == ' ')
          p++;
        if (*p == 0)
          break;
        p1 = strstr(p, " ");
        if (p1)
          *p1 = 0;

        sscanf(p, "%hhu ", &buffer[size]);
        size++;

        p = p1 ? p1+1 : NULL;
      }

      if (config.load_trees == 0)
      {
        if (TOS_NODE_ID == BASESTATION_ADDR)
          enqueue_uartmsg(0, size, buffer, TOS_NODE_ID);
      } else dohandle_summaryspec(TOS_NODE_ID, size, buffer);
    } else if (strcmp(s, "QUERY_SENDTOROOT") == 0)
    {
      sscanf(s1, "%hhu", &t);
      if (t) config.defaultquery_options |= QUERYOPT_SENDTOROOT;
    } else if (strcmp(s, "QUERY_PRUNE") == 0)
    {
      sscanf(s1, "%hhu", &t);
      if (t) config.defaultquery_options |= QUERYOPT_PRUNE;
    } else if (strcmp(s, "QUERY_MULTICAST") == 0)
    {
      sscanf(s1, "%hhu", &t);
      if (t) config.defaultquery_options |= QUERYOPT_MULTICAST;
    } else if (strcmp(s, "QUERY_CACHEMULTICAST") == 0)
    {
      sscanf(s1, "%hhu", &t);
      if (t) config.defaultquery_options |= QUERYOPT_CACHEMULTICAST;
    } else if (strcmp(s, "QUERY_POPULATEWINDOW") == 0)
    {
      sscanf(s1, "%hhu", &t);
      if (t) config.defaultquery_options |= QUERYOPT_POPULATEWINDOW;
    } else if (strcmp(s, "QUERY_ESTSEL") == 0)
    {
      sscanf(s1, "%hhu", &t);
      if (t) config.defaultquery_options |= QUERYOPT_ESTSEL;
    } else if (strcmp(s, "PAUSE") == 0)
      sscanf(s1, "%hhu", &config.pause);
    else if (strcmp(s, "DISABLE_MSGLOG") == 0)
      sscanf(s1, "%hhu", &config.disable_msglog);
    else if (strcmp(s, "CONFIRM_DROP") == 0)
      sscanf(s1, "%hhu", &config.confirm_drop);
    else if (strcmp(s, "MERGE_PATHS") == 0)
      sscanf(s1, "%hhu", &config.merge_paths);
    else if (strcmp(s, "REPAIR_LEVEL") == 0)
      sscanf(s1, "%hhu", &config.repair_level);
    else if (strcmp(s, "FAIL_NODES") == 0)
    {
      p = s1;
      while (p)
      {
        while (*p == ' ')
          p++;
        if (*p == 0)
          break;
        p1 = strstr(p, " ");
        if (p1)
          *p1 = 0;

        sscanf(p, "%hu:%lu ", &node_id, &fail_time);

        if (node_id == TOS_NODE_ID)
          config.fail_time = fail_time;
        config.fail_nodes[config.fail_count].node = node_id;
        config.fail_nodes[config.fail_count].time = fail_time;
        config.fail_count++;
        if (config.fail_count >= MAX_FAILNODES)
          break;

        p = p1 ? p1+1 : NULL;
      }
    } else if (strcmp(s, "ENABLESEND_NODES") == 0)
    {
      config.enable_send = 0;
      p = s1;
      while (p)
      {
        while (*p == ' ')
          p++;
        if (*p == 0)
          break;
        p1 = strstr(p, " ");
        if (p1)
          *p1 = 0;

        sscanf(p, "%hu ", &node_id);
        enable_count++;
        if (node_id == TOS_NODE_ID)
        {
          config.enable_send = 1;
          break;
        }

        p = p1 ? p1+1 : NULL;
      }

      if (enable_count == 0)
        config.enable_send = 1;
    } else if (strcmp(s, "DIR_TREE") == 0)
      strcpy(config.tree_dir, s1);
    else if (strcmp(s, "DIR_VALUE") == 0)
      strcpy(config.value_dir, s1);
    else if (strcmp(s, "DIR_JOIN") == 0)
      strcpy(config.join_dir, s1);
    else if (strcmp(s, "DIR_SAMPLE") == 0)
      strcpy(config.sample_dir, s1);
    else if (strcmp(s, "DIR_ROUTE") == 0)
      strcpy(config.route_dir, s1);
  }

  fclose(f);
}
#endif

void init_mconfig()
{
  memset(&config, 0, sizeof(config));

#ifdef TOSSIM
  strcpy(config.tree_dir, "trees");
  strcpy(config.value_dir, "values");
  strcpy(config.join_dir, "joins");
  strcpy(config.sample_dir, "samples");
  strcpy(config.route_dir, "route");
#endif

  config.load_trees = 0; config.start_route = 0; config.start_join = 0; config.route_method = 1; 
  config.trees_enabled = TREE_COUNT;
  config.defaultquery_options = 0;
  config.disable_msglog = 0; config.pause = 0;
  config.node_count = 0; config.retry_count = 100; config.repair_level = REPAIR_L1;
  config.merge_paths = 2;
  config.enable_send = 1; config.confirm_drop = 1;
}



